package org.dicker.controller;

import java.io.File;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.dicker.biz.MessageBiz;
import org.dicker.biz.ReplyBiz;
import org.dicker.biz.UserBiz;
import org.dicker.biz.impl.MessageBizImpl;
import org.dicker.biz.impl.ReplyBizImpl;
import org.dicker.biz.impl.UserBizImpl;
import org.dicker.entity.MessageInfo;
import org.dicker.entity.ReplyInfo;
import org.lanqiao.mvc.entity.ModelAndView;
import org.lanqiao.mvc.entity.MultipartFile;
import org.lanqiao.mvc.entity.YockMvcAnnotation.Controller;
import org.lanqiao.mvc.entity.YockMvcAnnotation.RequestMapping;
import org.lanqiao.mvc.entity.YockMvcAnnotation.RequestParam;
import org.lanqiao.mvc.entity.YockMvcAnnotation.ResponseBody;
import org.lanqiao.mvc.entity.YockMvcAnnotation.ResponseDispatch;
import org.lanqiao.mvc.entity.YockMvcAnnotation.UploadFile;

@Controller("/reply")
public class ReplyController {
	
	private ReplyBiz rBiz=new ReplyBizImpl();
	private MessageBiz mBiz=new MessageBizImpl();

	@RequestMapping(value = "/reply_list.do") 
	@ResponseDispatch(value = "/WEB-INF/console/reply/reply_list.jsp")
	public ModelAndView index(
			HttpServletRequest req,
			HttpServletResponse res,
			@RequestParam(name = "page") String page){
		ModelAndView mv = new ModelAndView();
		
		
		int pageSize=5;
		int newpage=0;
		if(page==null){
			page="1";
		}
		newpage=Integer.parseInt(page);
		List<ReplyInfo> rList = rBiz.selectByList(newpage, pageSize);
		mv.addObject("rList", rList);
		
		try {
			int totalcount = rBiz.selectCount(-1);
			int totalpage=totalcount%pageSize==0 ? totalcount/pageSize : totalcount/pageSize+1;
			mv.addObject("totalpage", totalpage);
			mv.addObject("newpage", newpage);
			
			////////////////////
			//每个页码组显示5个
			int groupSize=5;
			
			//根据当前第几页求出位于第几组
			int currentGroup = (newpage-1)/groupSize + 1;
				
			//计算出当前这一组应该显示的页码范围
			//（startPage  ~  endPage）
			//（1  ~ 5）	   第1组
			//（6 ~ 10）	   第2组
			//（11 ~ 15）	   第3组
			//（16 ~ 20）	   第4组
			int startPage = (currentGroup-1)*groupSize+1;
			int endPage = currentGroup*groupSize;
			mv.addObject("startPage", startPage);
			mv.addObject("endPage", endPage);
			////////////////////
			
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return mv;
	}
	
	
	@RequestMapping(value = "/reply_addpage.do") 
	@ResponseDispatch(value = "/WEB-INF/console/reply/reply_add.jsp")
	public ModelAndView replyAddPage(
			HttpServletRequest req,
			HttpServletResponse res
			)
					throws Exception{
		ModelAndView mv = new ModelAndView();
		
		return mv;
	}
	
	@RequestMapping(value = "/reply_add.do")
	@UploadFile
	@ResponseBody
	public String replyAdd(
			HttpServletRequest req,
			HttpServletResponse res,
			@RequestParam(name = "textarea") String textarea,
			@RequestParam(name = "title_id") String title_id,
			@RequestParam(name = "user_id") String user_id
					)throws Exception{
		
		req.setCharacterEncoding("utf-8");
		res.setCharacterEncoding("utf-8");
		res.setContentType("text/html; charset=UTF-8");
		
		
		String str="";
		if(mBiz.selectByInfo(Integer.parseInt(title_id))!=null && rBiz.selectUserid(Integer.parseInt(user_id))){
			ReplyInfo rInfo=new ReplyInfo();
			rInfo.setContent(textarea);
			rInfo.setMessage_id(Integer.parseInt(title_id));
			rInfo.setUser_id(Integer.parseInt(user_id));
			
			if(rBiz.addReply(rInfo)){
				str="<script>alert('添加成功');parent.location.href='/big-birds/reply/reply_list.do'</script>";
			}else{
				str="<script>alert('添加失败!请重试');parent.location.reload()</script>";
			}
		}else{
			str="<script>alert('用户或资讯不存在,请重试');parent.location.reload()</script>";
		}
		

		return str;
	}
	
	@RequestMapping(value = "/reply_delete.do") 
	@ResponseBody
	public String deleteById(
			HttpServletRequest req,
			HttpServletResponse res,
			@RequestParam(name = "replyid") int replyid)
					throws Exception{
		req.setCharacterEncoding("utf-8");
		res.setCharacterEncoding("utf-8");
		res.setContentType("text/html; charset=UTF-8");
		int num = rBiz.deleteById(replyid);
		String str="";
		if(num==1){
			str="<script>alert('删除成功');location.href='/big-birds/reply/reply_list.do'</script>";
		}else{
			str="<script>alert('删除失败!请重试');location.reload()</script>";
		}
		
		return str;
	}
	
	@RequestMapping(value = "/reply_deleteall.do") 
	@ResponseBody
	public String deleteByIdAll(
			HttpServletRequest req,
			HttpServletResponse res,
			@RequestParam(name = "replyids") String replyid)
					throws Exception{
		req.setCharacterEncoding("utf-8");
		res.setCharacterEncoding("utf-8");
		res.setContentType("text/html; charset=UTF-8");
		
		String[] aaa=replyid.split(",");
		int num = 0;
		for (String string : aaa) {
			num = rBiz.deleteById(Integer.parseInt(string));
		}
		String str=num+"";
		
		return str;
	}
	
	@RequestMapping(value = "/reply_updatepage.do") 
	@ResponseDispatch(value = "/WEB-INF/console/reply/reply_update.jsp")
	public ModelAndView replyUpdatePage(
			HttpServletRequest req,
			HttpServletResponse res,
			@RequestParam(name = "id") int id)
					throws Exception{
		ModelAndView mv = new ModelAndView();
		
		ReplyInfo rInfo = rBiz.selectByInfo(id);
		mv.addObject("rInfo", rInfo);
		return mv;
	}
	
	
	@RequestMapping(value = "/reply_update.do")
	@UploadFile
	@ResponseBody
	public String replyUpdate(
			HttpServletRequest req,
			HttpServletResponse res,
			@RequestParam(name = "textarea") String textarea,
			@RequestParam(name = "id") int id)
					throws Exception{
		
		req.setCharacterEncoding("utf-8");
		res.setCharacterEncoding("utf-8");
		res.setContentType("text/html; charset=UTF-8");
		
		
		ReplyInfo rinfo=new ReplyInfo();
		rinfo.setContent(textarea);
		rinfo.setReply_id(id);
		
		String str="";
		if(rBiz.updateById(rinfo)){
			str="<script>alert('修改成功');parent.location.href='/big-birds/reply/reply_list.do'</script>";
		}else{
			str="<script>alert('修改失败!请重试');parent.location.reload()</script>";
		}

		return str;
	}

}
